package com.teame.readbaby;

import java.util.List;


import com.teame.readbaby.po.Cart;
import com.teame.readbaby.po.Product;
import com.teame.readbaby.util.GloableParams;



import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ShoppingCartActivity extends Activity implements OnClickListener {

	private Button bt_editCart;
	private Button bt_toCheck;
	private TextView tv_cart_total_num;
	private TextView tv_cart_total_jifen;
	private TextView tv_cart_total_price;
	private ListView listView;
	
	private LinearLayout ll_youhui;
	private TextView tv_youhui;
	private Button bt_home;
	private Button bt_search;
	private Button bt_classify;
	private Button bt_cart;
	private TextView tv_show_cart_num;
	private Button bt_more;
	private MyAdapter adapter;

	Cart cart = null;
	private TextView tv_goto_shopping;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		GloableParams.defaultIndex=4;
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		cart = new Cart();

		// TODO:界面的设置：思路：应该将Product中的数据通过公式来转化比较好
		/*if (cart.getProductList().size() == 0) {
			// 显示空的购物车界面
			setContentView(R.layout.shopping_cart_nothing);
		
			tv_goto_shopping = (TextView) findViewById(R.id.tv_goto_shopping);
			initBottom();//初始化底部导航
			tv_show_cart_num.setText("0");
			tv_goto_shopping.setOnClickListener(this);

		} else {
			// 显示有商品的购物车界面
			setContentView(R.layout.shopping_car_activity);
			initHeader();//初始化头
			initContent();//初始化中间内容体
			initBottom();//初始化底部导航
		}*/
		setContentView(R.layout.shopping_car_activity);
		initHeader();//初始化头
		initContent();//初始化中间内容体
		initBottom();//初始化底部导航
		
	}
	
	
	

	/**
	 * 初始化头的组件
	 */
	private void initHeader() {
		bt_editCart = (Button) findViewById(R.id.bt_editCart);
		bt_toCheck = (Button) findViewById(R.id.bt_toCheck);
		
		bt_editCart.setOnClickListener(this);
		bt_toCheck.setOnClickListener(this);
	}
	
	/**
	 * 初始化中间体
	 */
	private void initContent() {
	
		tv_cart_total_num = (TextView) findViewById(R.id.tv_cart_total_num);
		tv_cart_total_jifen = (TextView) findViewById(R.id.tv_cart_total_jifen);
		tv_cart_total_price = (TextView) findViewById(R.id.tv_cart_total_price);

		listView = (ListView) findViewById(R.id.listView);

		ll_youhui = (LinearLayout) findViewById(R.id.ll_youhui);
		tv_youhui = (TextView) findViewById(R.id.tv_youhui);

		// 为cart界面的数据设置数据
		setInit();
					
		// 设置监听
		tv_youhui.setOnClickListener(this);
		

		adapter = new MyAdapter();
		listView.setAdapter(adapter);
		
		//TODO:修改商品数量更改bean中的数据及更新界面
		//TODO:删除对应的商品更改更新界面
	}
	
	
	/**
	 * 设置cart中的相关的公共信息
	 */
	private void setInit() {
		/*tv_cart_total_num.setText(cart.getCartNum());
		tv_cart_total_jifen.setText(cart.getCartJifen());
		tv_cart_total_price.setText(cart.getCartPrice());
		//tv_show_cart_num.setText(cart.getCartNum());
*/		
		tv_cart_total_num.setText("10");
		tv_cart_total_jifen.setText("1200");
		tv_cart_total_price.setText("￥1200");
		//tv_show_cart_num.setText(cart.getCartNum());

	}
	
	
	
	/**
	 * 初始化底部导航
	 */
	private void initBottom() {
		
		bt_home = (Button) findViewById(R.id.bt_home);
		bt_search = (Button) findViewById(R.id.bt_search);
		bt_classify = (Button) findViewById(R.id.bt_classify);
		bt_cart = (Button) findViewById(R.id.bt_cart);
		// 此处特别
		tv_show_cart_num = (TextView) findViewById(R.id.tv_show_cart_num);
		bt_more = (Button) findViewById(R.id.bt_more);
		
		bt_home.setOnClickListener(this);
		bt_search.setOnClickListener(this);
		bt_classify.setOnClickListener(this);
		bt_cart.setOnClickListener(this);
		bt_more.setOnClickListener(this);
		
	}

	
	private class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			//cart.getProductList().size()
			return 5;
		}

		@Override
		public Object getItem(int position) {
			return cart.getProductList().get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view;
			ViewHolder holder = null;
			if (convertView != null & convertView instanceof LinearLayout) {
				view = convertView;
				holder = (ViewHolder) view.getTag();

			} else {
				holder = new ViewHolder();
				view = View.inflate(getApplicationContext(),
						R.layout.shopping_car_listitem, null);
				holder.tv_product_name = (TextView) view
						.findViewById(R.id.tv_product_name);

				holder.tv_product_num = (TextView) view
						.findViewById(R.id.tv_product_num);
				holder.tv_product_price = (TextView) view
						.findViewById(R.id.tv_product_price);
				holder.tv_product_total_price = (TextView) view
						.findViewById(R.id.tv_product_total_price);

				holder.tv_product_size = (TextView) view
						.findViewById(R.id.tv_product_size);
				holder.tv_product_color = (TextView) view
						.findViewById(R.id.tv_product_color);

				holder.iv_product_image = (ImageView) view
						.findViewById(R.id.iv_product_image);
			

				view.setTag(holder);
			}

			List<Product> list = cart.getProductList();
			for (Product product : list) {
				// 获取网络图片
				/*
				 * Bitmap bit = BitmapFactory.decodeStream(InputStream is);
				 * holder.iv_product_image.setImageBitmap(productList.getPic());
				 */
				holder.iv_product_image.setImageResource(R.drawable.product_01);
				holder.tv_product_name.setText(product.getName());
				holder.tv_product_num.setText(product.getNumber());
				holder.tv_product_size.setText(product.getSize());
				holder.tv_product_color.setText(product.getColor());
				holder.tv_product_price.setText(product.getPrice());
				holder.tv_product_total_price.setText(product.getTotalPrice());
				
			}
			return view;
		}

	}

	static class ViewHolder {
		private TextView tv_product_name;
		private TextView tv_product_num;
		private TextView tv_product_size;
		private TextView tv_product_color;
		private TextView tv_product_price;
		private TextView tv_product_total_price;
		private ImageView iv_product_image;

		
		
	}



	// 按钮点击事件的设置
	@Override
	public void onClick(View v) {
		// TODO 购物车导航点击事件的处理;在跳转到其他页面之前应该数据返回到服务器端
		/*
		 * bt_home.setOnClickListener(this); bt_search.setOnClickListener(this);
		 * bt_classify.setOnClickListener(this);
		 * bt_cart.setOnClickListener(this); bt_more.setOnClickListener(this);
		 */
		Intent intent = null;
		switch (v.getId()) {

		case R.id.bt_home:// 首页界面
			 intent= new Intent(this,HomeActivity.class);
			startActivity(intent);

			break;

		case R.id.bt_search:// 搜索界面
			intent = new Intent(this,SearchActivity.class);
			startActivity(intent);

			break;
		case R.id.bt_classify:// 分类界面
			 intent = new Intent(this,BrandListActivity.class);
			startActivity(intent);

			break;
		case R.id.bt_cart:// 购物车界面
			intent = new Intent(ShoppingCartActivity.this, ShoppingCartActivity.class);
			startActivity(intent);

			break;
		case R.id.bt_more:// 更多界面
			 intent = new Intent(this,MoreActivity.class);
			startActivity(intent);

			break;
		case R.id.tv_youhui:// 跳转到活动优惠的（促销商品的界面）
			intent = new Intent(this,TopicProductlistActivity.class);
			startActivity(intent);

			break;

		case R.id.bt_editCart:// 编辑购物车页面
			// intent = new Intent(MainActivity.this,"");
			//startActivity(intent);
			Toast.makeText(getApplicationContext(), "商品修改", 0).show();

			break;
		case R.id.bt_toCheck:// 跳转到结算中心的界面
			 intent = new Intent(this,PaymentCenterAactivity.class);
			startActivity(intent);

			break;
		case R.id.tv_goto_shopping:// 去购物
			 intent = new Intent(this,ProductListActivity.class);
			startActivity(intent);
			break;

		}

	}

}
